package com.peanut.devlibrary.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Created by anonymouslystudio on 2017/6/6.
 */

public class MathUtil {

    /**
     * get current process
     * @param current
     * @param total
     * @return
     */
    public static int getProgress(long current, long total) {
        if(total == 0){
            return 0;
        }
        double percent = div(current, total);
        return (int) (percent * 100);
    }

    /**
     * get discount
     * @param totalPrice
     * @param realPay
     * @return
     */
    public static double getDiscount(int totalPrice,int realPay){
        double discount = ((double) realPay) / totalPrice * 10;
        DecimalFormat format = new DecimalFormat("0.0");
        String result = format.format(discount);
        double resultDouble = Double.parseDouble(result);

        return resultDouble;
    }

    public static double div(double v1, double v2) {
        return div(v1, v2, 2);
    }

    private static double div(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));

        double result = b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();

        return result;
    }

    /**
     * 产生4位随机数
     * @return
     */
    public static  long generateRandomNumber(int n){
        if(n<1){
            throw new IllegalArgumentException("随机数位数必须大于0");
        }
        return (long)(Math.random()*9*Math.pow(10,n-1)) + (long)Math.pow(10,n-1);
    }
}
